package Comunication;

import java.util.LinkedList;
import java.util.List;

public class Simulator {
	private static  List<Memory> memList;
	static int wordProcessor = 16;
	public Simulator(){
		
	}
	
	/*
	 * cacheType:	id	type
	 * 				0	direct mapped
	 * 				1	set associative 2-way
	 */	
	public static List<Memory> setMemory(int size, int bitTag, int blockSize, int CacheBlockNum, int cacheType){
		int nBlock = (size / blockSize);
		String padding = "";
		for(int k=0; k<blockSize; k++)
			padding += "0";
		switch(cacheType){
		case 0:{	// direct mapped
			int shift = (int) Math.pow(2,(double) (wordProcessor-bitTag)); 
			for(int i=0; i<nBlock; i++){
				int address = i*shift;	// tag
				int blocco = i % CacheBlockNum;
				String strIndex = Integer.toBinaryString(blocco);
				int y=3;
				for(int j=strIndex.length();j>0;j--){
					address += ( ((int)strIndex.charAt(j-1)) -48) * ((int)Math.pow(2, (double)y));			
					y++;
				}
				Memory myMem = new Memory(Integer.toHexString(address),padding);
				memList.add(myMem);
			}
			return memList;
		}
		case 1:{	// set associative 2-way
			return memList;
		}
		}
		return null;
	}
	
	public static int getDirectMappedBlock(String address, int startIndex) {
		String bin = Integer.toBinaryString(Integer.parseInt(address,16));
		if (bin.equals("0")){
			System.out.println("torno 0");
			return 0;
		}
		return Integer.parseInt(bin.substring(bin.length()-startIndex, bin.length()-3),2);
	}

	/*public int getAssociativeBlock(String address, int startIndex) {
		
	}*/
	
	/*public int getSetAssociativeBlock(String address, int startIndex, int nBlock) {
		String bin = Integer.toBinaryString(Integer.parseInt(address,16));
		int n= Integer.parseInt(bin.substring(bin.length()-startIndex, bin.length()-3),2);
		n = (n % (nBlock/2))*2;
		if(blocksList.get(n).used==false)
			return n;
		else
			return n+1;
	}*/
	
	public static void main(String[] args) {
		memList = new LinkedList<Memory>();
		List<Memory> mm = setMemory(128,11,2,4,0);		
		for(int i=0; i<mm.size(); i++){
			System.out.println(i+" - "+mm.get(i).label);
		}

	}

}
